package com.le.tester.book.flightjava8.lambda;

import com.google.common.collect.Lists;
import com.le.tester.book.flightjava8.parameterpassing.Apple;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Supplier;

/**
 * createTime：2025-06-21 14:57
 * description：
 */
public class Demo {

    public static String processFile(BufferedReaderProcessor p) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
            return p.process(br);
        }
    }

    //    正常写法
    public static <T> List<T> filter(List<T> list, Predicate<T> p) {
        ArrayList<T> result = new ArrayList<>();

        for (T t : list) {
            if (p.test(t)) {
                result.add(t);
            }
        }
        return result;
    }

    public static <T> void forEach(List<T> list, Consumer<T> c) {
        for (T t : list) {
            c.accept(t);
        }
    }

    public static <T, R> List<R> map(List<T> list, Function<T, R> f) {
        ArrayList<R> result = new ArrayList<>();
        for (T t : list) {
            result.add(f.apply(t));
        }
        return result;
    }


    public static void main(String[] args) throws IOException {
//        这里的bufferReadProcessor是一个接口，那么就可以使用lambda表达式了
        Demo.processFile(BufferedReader::readLine);

        Predicate<String> p = (String s) -> !s.isEmpty();
        List<String> filter = filter(Lists.newArrayList("1"), p);

        Demo.forEach(Lists.newArrayList(1, 2), System.out::println);

        Demo.map(Lists.newArrayList("in"), String::length);

//        可以使用一个函数式接口来接收,new的关键字，一点代表着调用了构造函数

        Supplier<Apple> c1 = Apple::new;
        Apple apple = c1.get();

        //Function<Integer, Apple> appleFunction = Apple::new;

        //BiFunction<String, Integer, Apple> c3 = Apple::new;

    }
}
